﻿using ELab.Communication.Messages.ControllerMessages;
using ELab.Core;
using ELab.Management;
using ELab.Serialization.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ELab.Teacher.Apps
{
    public partial class ChangePassForm : Form
    {
        public ChangePassForm()
        {
            InitializeComponent();
        }
        public ELabController Controller { get; set; }
        public User CurrentUser { get; set; }
        private string errorMsg = "There are an error when process changing password.";
        private void btnChangePass_Click(object sender, EventArgs e)
        {
            if (CurrentUser == null)
            {
                MessageBox.Show(errorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
                return;
            }

            if (CurrentUser.Password != Algorithm.SHAPassword(txtPasswordOld.Text))
            {
                MessageBox.Show(errorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
                return;
            }

            if (txtPassword.Text != txtPasswordConfirm.Text)
            {
                MessageBox.Show(errorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
                return;
            }

            CurrentUser.Password = Algorithm.SHAPassword(txtPassword.Text);
            //Send RemoveApplicationMessage to ELab server 
            Controller.Connect();
            var changePass = Controller.SendMessageAndGetResponse(new UpdateUserMessage
            {
                User = CurrentUser
            });
            Controller.Disconnect();
            if (((GeneralResponseMessage)changePass).IsSuccess)
            {
                this.Close();
            }
        }
    }
}
